
#include "Public.h"
#include "stm32f0xx.h"

void USART2_Init(void)
{
    // 使能USART2和GPIOA时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
    // 配置GPIO引脚
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

    GPIO_InitTypeDef GPIO_InitStruct;
	
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1); // 配置引脚2为USART2的复用功能
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1); // 配置引脚3为USART2的复用功能    

    
    // 配置USART2中断

    
    NVIC_InitTypeDef  NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //通道设置为串口1中断（故后面应选择在“void USART2_IRQHandler(void)”开中断）
    NVIC_InitStructure.NVIC_IRQChannelPriority=0; //中断占先等级0
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断
    NVIC_Init(&NVIC_InitStructure);
//最后，可在stm32f10x_.it.c中的void USART2_IRQHandler(void)函数里写上串口中断发生时中断服务函数即可；   
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // 开启串口接受中断
    
    
    
    // 配置USART2
    USART_InitTypeDef USART_InitStruct;
    USART_InitStruct.USART_BaudRate = BaudRate_internet_H*65536+BaudRate_internet_L;//115200;   
    USART_InitStruct.USART_WordLength = USART_WordLength_8b;
    USART_InitStruct.USART_StopBits = USART_StopBits_1;
    USART_InitStruct.USART_Parity = USART_Parity_No;
    USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

    USART_Init(USART2, &USART_InitStruct);

    // 使能USART2
    USART_Cmd(USART2, ENABLE);
}
